/*FsQ8hq0RssViIid5Y1QU5HghrbO68RAcO9UOiZFZIVDgPuY7bUSBUn7FpGZV0Bvl*/
import React, { Component } from 'react';
import { createPage, base,ajax ,toast} from 'nc-lightapp-front';
let { NCButton, NCModal, NCStep ,NCMessage,NCSelect,NCCheckbox} = base;
const NCOption = NCSelect.NCOption;
import OptionSelect from './OptionSelect';
import AssignFilter  from './AssignFilter';
import OrgSelect from './OrgSelect';
import ResultGrid from './ResultGrid';

let EMPTY_FN = function(){};
class AssignStepModal extends Component {
    constructor(props) {
        super(props);

        this.state = {
            myModal:{
                modalDropup: true,
                size: 'xlg',
                backdrop:false/"static",//是否弹出遮罩层/点击遮罩层是否触发关闭事件
                modalTitle:'选择操作',
                fieldid:'assignstepmodal_modal-area'
            },
            step:{
                current: 0,
                stepCount: 4
            },
            show:false,
            showResultGrid:true
        }
        this.onFinish = this.props.onFinish || EMPTY_FN;
        this.config = props.config;
        this.config.isAssign = true;
    }
    loadSaleInfoGridData = ()=>{
        let editStatusBtns = ['subG5ModalSave','subG5ModalCancel'];
        let browseStatusBtns=['subGrid5Edit','subGrid5Del','subGrid5Addr','subGrid5Ref','subGrid5Print']
        this.props.loadSubGridData&&this.props.loadSubGridData(
            'shoulder',
            this.config.subGrid5,
            this.config.custsaleForm,
            this.config.pagecode,
            'querySubFormOrGrid',
            editStatusBtns,
            browseStatusBtns
        );
    }
    loadFinaceInfoGridData = ()=>{
        let editStatusBtns = ['subG4ModalSave','subG4ModalCancel'];
        let browseStatusBtns=['subGrid4Edit','subGrid4Del','subGrid4Ref','subGrid4Pri'];
        this.props.loadSubGridData&&this.props.loadSubGridData(
            'shoulder',
            this.config.subGrid4,
            this.config.custfinanceForm,
            this.config.pagecode,
            'querySubFormOrGrid',
            editStatusBtns,
            browseStatusBtns
        )
    }
    loadCreDitCtlInfoGridData =()=>{
        let editStatusBtns = ['subG6ModalSave','subG6ModalCancel'];
        let browseStatusBtns=['subG6Edit','subG6Del','subG6Ref','subG6Pri']
        this.props.loadSubGridData&&this.props.loadSubGridData(
            'shoulder',
            this.config.subGrid6,
            this.config.creditctlForm,
            this.config.pagecode,
            'querySubFormOrGrid',
            editStatusBtns,
            browseStatusBtns
        )
    }
    onsubmit=()=>{
       let checkRows = this.props.table.getCheckedRows(this.props.config.custassign);
       let assignFlag = this.config.isAssign;
       let targetOrgIds = [];
       let CustPks = [];
        this.orgSelect.state.selectData.datas.map((obj)=>{
            targetOrgIds.push(obj['id']);
        });
        if(checkRows&&checkRows.length>0){
            checkRows.map((obj)=>{
                CustPks.push(obj.data.values.pk_customer.value);
            })
        }else{
            toast({'color':'warning','content':assignFlag?'请选择要分配的客户！':'请选择要取消分配的客户！'});
            return;
        }
        ajax({
            url:'/nccloud/uapbd/customer/custAssign.do',
            data:{
                pkcustList:CustPks,
                targetOrgIds:targetOrgIds,
                isAssign:assignFlag
            },
            success:(res)=>{
                let{success,data} =res;
                if(success){
                    if(data&&data.hasOwnProperty('message')&&data.message){
                        toast({'color':'success','content':data.message});
                        this.loadFinaceInfoGridData();
                        this.loadSaleInfoGridData();
                        this.loadCreDitCtlInfoGridData();
                        this.cancel();
                    }
                }
            }
        });
    }
    cancel=() =>{
        this.config.isAssign = true;
        this.state.show =false;
        this.state.step.current =0;
        this.setState(
            this.state);
    }
    oncheckAssignOrCancel = (flag)=>{
        this.config.isAssign = flag ==='assign'?true:false;
    }

    loadResutlGridData = (qryparam)=>{

        let custassign = this.props.config.custassign;

        ajax({
            url:'/nccloud/uapbd/customer/custAssignResult.do',
            data:qryparam,
            success:(res)=>{
                let{success, data} =res;
               if(success){
                   if(data){
                       if(data.hasOwnProperty(custassign)){
                           this.props.table.setAllTableData(custassign,data[custassign])
                       }
                   }else{
                       this.props.table.setAllTableData(custassign,{
                           rows:[]
                       });
                   }
               }
            }
        });

    }

    onStep = (dir) => {
            let isAssign = this.config.isAssign;
            let current = this.state.step.current;
            let stepCount = this.state.step.stepCount;
            let modalTitle = this.state.myModal.modalTitle;
            let orgDatas = this.orgSelect.state.selectData.datas
            if(dir < 0 && current > 0){ //prev step
                current-=1;
            }
            if(dir > 0 && current < stepCount-1){ // next
                current+=1;
            }
            if(current===0){
                modalTitle = '选择操作';
            }

            if(current===1){
                modalTitle = isAssign?'选择分配目标组织':'选择取消分配目标组织';
            }else if(current===2){
                modalTitle = '过滤条件';
                if(orgDatas.length===0){
                    NCMessage.create({content: '请选择目标组织！', color: 'warning'});
                    return;
                }
            }else if(current===3){
                modalTitle = '数据选择';
                let searchVal = this.props.search.getAllSearchData(this.props.config.searchId_modal);
                let pageInfo = this.props.table.getTablePageInfo(this.props.config.custassign);
                if(!searchVal){
                    return;
                }
                let qryparam = {
                    pageInfo:pageInfo,
                    custcondition:{
                        conditions:[{
                            field:'NODE_TYPE',
                            value:{
                                firstvalue:this.config.NODE_TYPE
                            },
                            oprtyp:''
                        }]

                    },
                    queryAreaCode:this.config.searchId_modal,
                    querycondition: searchVal,
                    oid:this.config.oid,
                    querytype:'tree'
                }
              this.loadResutlGridData(qryparam,()=>{

              });
            }else{
                modalTitle = '选择操作';
            }
            this.state.step.current = current;
            this.state.myModal.modalTitle = modalTitle;
            this.setState(this.state);
    };
    show = ()=>{
        this.state.show = true;
        this.setState(this.state);
    }
    handleChange = ()=>{

    }
    changeCheck=(value)=>{
        console.log(value)
        this.state.showResultGrid =value;
        this.setState(this.state);

    }

    render() {
        const config = this.state.myModal;
        return (
            <NCModal {...config} show={this.state.show} onHide = {this.cancel.bind(this)} fieldid = 'assignstep'>
                <NCModal.Header closeButton={true}>
                    <NCModal.Title>{this.state.myModal.modalTitle}</NCModal.Title>
                </NCModal.Header>
                <NCModal.Body>
                        <NCStep.NCSteps {...this.state.step}>
                            <NCStep title="步骤1"  description="选择分配或者取消分配操作" />
                            <NCStep title="步骤2"  description="选择目标组织" />
                            <NCStep title="步骤3"  description="输入过滤条件" />
                            <NCStep title="步骤4"  description="选择分配或者取消分配的数据" />
                        </NCStep.NCSteps>
                        <div style={{height: '450px',display: this.state.step.current == 0 ? '': 'none'}}><OptionSelect oncheckAssignOrCancel ={this.oncheckAssignOrCancel.bind(this) } {...this.config}/></div>
                        <div style={{height: '450px',display: this.state.step.current == 1 ? '': 'none'}}><OrgSelect ref={( item )=> this.orgSelect = item}/></div>
                        <div style={{height: '450px',display: this.state.step.current == 2 ? '': 'none'}}><AssignFilter {...{pageprops:this.props}}/></div>
                        <div style={{height: '450px',display: this.state.step.current == 3 ? '': 'none'}}><ResultGrid {...{pageprops: this.props}}/></div>
                </NCModal.Body>
                <NCModal.Footer>
                    <div  style={{display: this.state.step.current == this.state.step.stepCount - 2  ? '' : 'none' }}>
                        <h4>分配状态</h4>
                        <NCSelect defaultValue="" style={{ width: 200, marginRight: 6 }} onChange={this.handleChange}>
                            <NCOption value=""></NCOption>
                            <NCOption value="0">已分配</NCOption>
                            <NCOption value="1">未分配</NCOption>
                        </NCSelect>
                        <NCCheckbox  colors="info" onChange={this.changeCheck}>是否显示查询结果</NCCheckbox>
                    </div>
                    <span style={{display: this.state.step.current == 0 ? 'none' : '' }}><NCButton onClick={ this.onStep.bind(this,-1) }>上一步</NCButton></span>
                    <span style={{display: this.state.step.current == this.state.step.stepCount - 2&&this.state.showResultGrid  ? '' : 'none' }} ><NCButton onClick={ this.onsubmit.bind(this) }>完成</NCButton></span>
                    <span style={{display: this.state.step.current == this.state.step.stepCount - 2&&this.state.showResultGrid  ? '' : 'none' }} ><NCButton onClick={ this.onsubmit.bind(this) }>完成并继续</NCButton></span>
                    <span style={{display: this.state.step.current == this.state.step.stepCount - 2&&this.state.showResultGrid  ? 'none' : '' }}><NCButton onClick={ this.onStep.bind(this,1) }>下一步</NCButton></span>
                    <NCButton onClick={ this.cancel.bind(this) }>取消</NCButton>
                </NCModal.Footer>
            </NCModal>
        );
    }
}
export default AssignStepModal;
/*FsQ8hq0RssViIid5Y1QU5HghrbO68RAcO9UOiZFZIVDgPuY7bUSBUn7FpGZV0Bvl*/
